package com.hs.handler;

import com.alibaba.fastjson.JSON;
import com.hs.enums.AppHttpCodeEnum;
import com.hs.utils.ResultResponse;
import com.hs.utils.WebUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
// 授权过程中出现异常
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResultResponse<?> result = ResultResponse.fail(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode(),AppHttpCodeEnum.NO_OPERATOR_AUTH.getMsg());
        String json = JSON.toJSONString(result);
        //处理异常
        WebUtil.renderString(response,json);
    }
}
